package registry

import (
	"gitee.com/mkwchecking/sr-core/yal/ymap"
	"github.com/go-kratos/kratos/v2/registry"
)

type (
	RegistrySet ymap.ISet[registry.Registrar]

	DiscoverySet ymap.ISet[registry.Discovery]
)

func NewRegistrySet(def ...registry.Registrar) RegistrySet {
	container := ymap.NewSetMap[registry.Registrar]()
	if len(def) > 0 {
		container.SetDefault(def[0])
	}
	return container
}
func NewDiscoverySet(def ...registry.Discovery) DiscoverySet {
	container := ymap.NewSetMap[registry.Discovery]()
	if len(def) > 0 {
		container.SetDefault(def[0])
	}
	return container
}

func New(registerSet RegistrySet, discoverySet DiscoverySet) IRegistry {
	return &registerCenter{
		registry:  registerSet,
		discovery: discoverySet,
	}
}

type IRegistry interface {
	GetRegistry() registry.Registrar

	GetDiscovery() registry.Discovery
}

type registerCenter struct {
	registry  RegistrySet
	discovery DiscoverySet
}

// GetDiscovery implements IRegistry.
func (r *registerCenter) GetDiscovery() registry.Discovery {
	val, _ := r.discovery.GetDefault()
	return val
}

// GetRegistry implements IRegistry.
func (r *registerCenter) GetRegistry() registry.Registrar {
	val, _ := r.registry.GetDefault()
	return val
}
